
function changePicture(imgsrc,imgName){
	document[imgsrc].src=""+imgName;
}

function changeAvatar(){
	document['registration']['avatar']=''+document['registration']['myImage'];
}

function changeVideo(id, linkvideo){
	document[id].src = ""+linkvideo;
}

function changeTheme(BGimg, bannerImg, fontt, fontsize, floating){
	document['banner'].src = ""+bannerImg;
	document['body'].style.backgroundImage = "url("+BGimg+")";
	document.getElementsByTagName('body')[0].style.fontFamily = ""+fontt;
	document.getElementsByTagName('body')[0].style.fontSize = ""+fontsize;
	document.getElementById('preview').style.cssFloat = ""+floating;
}

function previewImage()
{
	//var path = document.forms["registration"]["myImage"].value;
	alert(document.forms["registration"]["myImage"].value);
	document.forms["registration"]["avatar"].value = document.forms["registration"]["myImage"].value;
	
}

function radio_tipe_change() {
	var tipe = getTipeCheck();
	var pathBox = document.getElementById("input_path");
	var descBox = document.getElementById("input_desc");
	var pathLabel = document.getElementById("fields").getElementsByTagName("div")[2];
	var descLabel = document.getElementById("fields").getElementsByTagName("div")[3];
		
	if ((tipe == "Link") || (tipe == "Video")) {
		if (tipe == "Link") {
			pathLabel.innerText = "URL";
			descLabel.innerText = "Deskripsi";
			descBox.innerHTML = '<input type="text" name="text_desc" size="40" />'
		} else {
			pathLabel.innerText = "URL YouTube";
			descLabel.innerText = "";
			descBox.innerHTML = "";
		}
		pathBox.innerHTML = '<input type="text" name="text_path" onKeyUp="cekYoutube();" onChange="cekYoutube();" onBlur="cekYoutube();" size="40" />';
		
	} else if (tipe == "Image") {
		pathLabel.innerText = "Path File";
		descLabel.innerText = "";
		pathBox.innerHTML = '<input type="file" name="text_path" />';
		descBox.innerHTML = "";
	}
}

function btn_kirim_click() {
	var judul = document.getElementsByName("text_judul")[0].value;
	var tipe = getTipeCheck();	
	
	document.getElementById("fields").style.display = "none";
	document.getElementById("errors").style.display = "none";
	document.getElementById("inputs").style.display = "none";
	document.getElementById("content_head").innerText = judul + " telah dipost dengan sukses!";
	
	document.getElementsByName("btn_kirim")[0].style.visibility = "hidden";
	
	var preview = document.getElementById("post_preview");
	if (tipe == "Link") {
		preview.innerHTML = "<b>URL:</b> " + document.getElementsByName("text_path")[0].value + "<br /><b>Deskripsi:</b> " + document.getElementsByName("text_desc")[0].value;
	} else if (tipe == "Image") {
		preview.innerHTML = '<img src="logo.png" />';
	} else if (tipe == "Video") {
		preview.innerHTML ='<object width="425" height="350"><param name="movie" value="'+konversiYoutubeURL(document.getElementsByName("text_path2")[0].value)+'"></param><param name="wmode" value="transparent"></param><embed src="'+konversiYoutubeURL(document.getElementsByName("text_path2")[0].value)+'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>'
		}
}

function getTipeCheck() {
	var radios = document.getElementsByName("radio_tipe");
	if (radios[0].checked) {
		return "Link";
	} else if (radios[1].checked) {
		return "Image";
	} else if (radios[2].checked) {
		return "Video";
	} else {
		return "";
	}
}

function writeToDoc(texttowrite){
	document.write(texttowrite);
}

function UsernameChange() {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("validusername").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("POST","php/ValidasiUsername.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("username="+document.getElementById('text_username').value);
}

function PasswordChange() {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("validpassword").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("POST","php/ValidasiPassword.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("username="+document.getElementById('text_username').value+"&password="+document.getElementById('text_password').value+"&email="+document.getElementById('text_email').value);
}

function loadSWF()
{
	alert('loadSWF');
	document.write("<div id=\"datepicker\" style=\"position:absolute; width:277px; height:271px; z-index:1; visibility: hidden;\" onmouseover=\"javascript:dpmouseover=true;\" onmouseout=\"javascript:dpmouseover=false;\"><object id	=\"fdatepicker\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" type=\"application/x-shockwave-flash\" width=\"230\" height=\"230\"><param name=\"movie\" value=\"swf/datepicker.swf\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"quality\" value=\"high\" /><param name=\"swfversion\" value=\"8.0.35.0\" /><embed name=\"fdatepicker\" wmode=\"transparent\" src=\"swf/datepicker.swf\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"230\" height=\"230\"></embed></object></div>");
}

function tryLogin() {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			if (xmlhttp.responseText=="Invalid username and password.") {
				document.getElementById("loginerror").innerHTML=xmlhttp.responseText;
			} else {
				window.location.href = "profil.php?id="+xmlhttp.responseText;
			}
		}
	}
	xmlhttp.open("POST","php/login.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("username="+document.getElementById('text_UserName_home').value+"&password="+document.getElementById('text_Password_home').value);
}

function PreviewPost() {
	var judul = document.getElementsByName("text_judul")[0].value;
	var tipe = getTipeCheck();	
	
	document.getElementById("fields").style.display = "none";
	document.getElementById("inputs").style.display = "none";
	document.getElementById("errors").style.display = "none";
	document.getElementById("content_head").innerText = "Preview";
	
	document.getElementsByName("btn_kirim")[0].style.visibility = "hidden";
	document.getElementsByName("btn_kirim")[2].style.visibility = "visible";
	
	var preview = document.getElementById("post_preview");
	if (tipe == "Link") {
		preview.innerHTML = "<b>URL:</b> " + "<a href=\"http://" + document.getElementsByName("text_path")[0].value + "\">" + document.getElementsByName("text_path")[0].value + "</a><br /><b>Deskripsi:</b> " + document.getElementsByName("text_desc")[0].value;
	} else if (tipe == "Image") {
		preview.innerHTML = 'nothing to preview<br/><br/>';
	} else if (tipe == "Video") {
	preview.innerHTML ='<object style="height: 390px; width: 640px"><param name="movie" value="'+document.getElementsByName("text_path2")[0].value +'"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="'+document.getElementsByName("text_path2")[0].value +'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="360"></object>';
	} else {
		preview.innerHTML = 'nothing to preview<br/><br/>';
	}
}

function CancelPost() {
	var judul = document.getElementsByName("text_judul")[0].value;
	var tipe = getTipeCheck();	
	
	document.getElementById("fields").style.display = "inline";
	document.getElementById("inputs").style.display = "inline";
	document.getElementById("content_head").innerText = "POST NEW CONTENT";
	
	document.getElementsByName("btn_kirim")[0].style.visibility = "visible";
	document.getElementsByName("btn_kirim")[2].style.visibility = "hidden";
	
	var preview = document.getElementById("post_preview");
	preview.innerHTML = "";
}

function getQueryOK(str) {
	str = str.replace(/%3B/gi,";");
	str = str.replace(/%3F/gi,"?");
	str = str.replace(/%2F/gi,"/");
	str = str.replace(/%3A/gi,":");
	str = str.replace(/%23/gi,"#");
	str = str.replace(/%26/gi,"&");
	str = str.replace(/%3D/gi,"=");
	str = str.replace(/%2B/gi,"+");
	str = str.replace(/%24/gi,"$");
	str = str.replace(/%2C/gi,",");
	str = str.replace(/%20/gi," ");
	str = str.replace(/%25/gi,"%");
	str = str.replace(/%3C/gi,"<");
	str = str.replace(/%3E/gi,">");
	str = str.replace(/%7E/gi,"~");
	str = str.replace(/%27/gi,"'");
	str = str.replace(/~/gi,"<br/>");
	return str;
}

function getURLQueryValueById(url,id) {
	// mengembalikan nilai dari parameter query string yang dimasukkan
	var str = url;
	var idx = str.indexOf(id) + id.length +1;
	var idx2 = str.indexOf("&", idx);
	if (idx2 == -1) {
		str = str.substring(idx);
	} else {
		str = str.substring(idx, idx2);
	}
	str = getQueryOK(str);
	return str;
}

function konversiYoutubeURL(url) {
	var str = "http://www.youtube.com/watch?feature=player_detailpage&v=";
	str += getURLQueryValueById(url,"v");
	return str;
}

function konversiYoutubeURLOK(url) {
	var str = "http://www.youtube.com/v/";
	str += getURLQueryValueById(url,"v");
	str += "?version=3&feature=player_detailpage";
	return str;
}

function cekYoutube() {
	document.getElementsByName("text_path2")[0].value = konversiYoutubeURLOK(document.getElementsByName("text_path")[0].value);
}